package com.file.controller;

import com.file.msg.RequestData;
import com.file.msg.RequestMsg;
import com.file.service.UseService;
import com.file.sys.SysMsg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
@CrossOrigin
public class UseController {
    @Autowired
    private UseService useService;

    /**
     * 进入文件夹
     * @param dir 目录
     * @return
     */
    @GetMapping("/cd")
    public RequestData cdDir(String dir){
        log.info("进入了cd");
        log.info("dir:"+dir);
//        if (SysMsg.path.get().equals(SysMsg.root))
//            return new RequestData("9999","没有更上级目录",null);
        if ("..".equals(dir)){
            int endDir = cPath(SysMsg.path.get());
            SysMsg.path.set(SysMsg.path.get().substring(0,endDir+1));
            log.info(SysMsg.path.get());
            return new RequestData(RequestMsg.SUCCESS_CD_DIR,SysMsg.path.get());
        } else if (dir != null){
            String p = SysMsg.path.get() + dir;
            log.info("path:"+p);
            File f = new File(p);
            if (f.exists()){
                if (f.isFile())
                    return new RequestData("9999","不是目录",null);
                SysMsg.path.set(p +"/");
                return new RequestData(RequestMsg.SUCCESS_CD_DIR,SysMsg.path.get());
            }else
                return new RequestData(RequestMsg.ERROR_NOT_EXSIST_DIR,null);
        }
        return new RequestData("9999","请输入文件夹",null);
    }
    public int cPath(String str){
        for (int i = str.length()-2; i > 0 ; i--) {
            if (str.charAt(i) == '/')
                return i;
        }
        return str.length();
    }

    /**
     * 上传文件
     * @param file 文件
     * @param dir   上传到哪个目录
     * @return
     */
    @PostMapping("/upload")
    public RequestData uploadFile(MultipartFile file, @RequestParam(required = false) String dir){
        return useService.upload(file,dir);
    }
    @DeleteMapping("/del")
    public RequestData delFile(String fileName){
        return useService.del(fileName);
    }
    /**
     * 测试
     * @return
     */
    @GetMapping("/test")
    public String test(){
        log.info("成功访问test接口库");
        return "访问成功";
    }

    /**
     * 创建目录
     * @param dirs
     * @return
     */
    @GetMapping("/mkdirs/{dirs}")
    public RequestData mkdirs(@PathVariable String dirs){
        File file;
        if (dirs!=null)
            file = new File(SysMsg.path.get() + dirs);
        else
            return new RequestData(RequestMsg.ERROR_EXSIST_DIR, null);

        log.info(file.getAbsolutePath());
        if (!file.exists()){
            file.mkdirs();
            return new RequestData(RequestMsg.SUCCESS_CREATE_File, null);
        }
        return new RequestData(RequestMsg.ERROR_EXSIST_DIR, null);
    }

    /**
     * 产看当前木中的文件
     * @param flist
     * @return
     */
    @GetMapping("/ls")
    public RequestData showFile(@RequestParam(required = false,defaultValue = "") String flist){
        log.info("进入了showFile");
        log.info("path:"+SysMsg.path.get());
        File f;
        if (flist != null && !flist.equals("")){
            f = new File(SysMsg.path.get() + flist);
            if (!f.exists())
                return new RequestData(RequestMsg.ERROR_NOT_EXSIST_DIR,null);
        }
        else
            f = new File(SysMsg.path.get());
        List<String> files = new ArrayList<>();
        files.add("当前目录:"+SysMsg.path.get()+flist);
        for (File file : f.listFiles()) {
            String str = file.isDirectory() ? "---dir" : "---file";
            files.add(file.getName()+str);
        }
        log.info("files:"+files);
        System.out.println(files);
        return new RequestData(RequestMsg.SUCCESS_Find_File, files);
    }

    /**
     * 删除文件
     * @param file
     * @return
     */
    @GetMapping("/rm")
    public RequestData deleteFile(String file){
        log.info("进入了dleteFile接口");
        log.info("file:"+file);
        File f;
        String fname = SysMsg.path.get() + file;
        if (file != null || !"".equals(file)){
            f = new File(fname);
            if (!f.exists())
                return new RequestData(RequestMsg.ERROR_NOT_EXSIST_DIR,null);
            deleteF(f);
        }
        return new RequestData(RequestMsg.SUCCESS_DELETE_File,null);
    }
    public void deleteF(File file){
        if (file.isDirectory()){
            for (File f : file.listFiles()) {
                if (f.isDirectory())
                    deleteF(f);
                else
                    f.delete();
            }
        }
        file.delete();
    }
}
